home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows 95 with MFC / Programming Windows 95 with MFC (Microsoft Programming Series)(097-0001465)(1996).iso / CODE / Chap06 / DlgDemo5 / DlgDemo5.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-05  |  7.1 KB  |  293 lines

  1. //***********************************************************************
  2. //
  3. //  DlgDemo5.cpp
  4. //
  5. //***********************************************************************
  6.  
  7. #include <afxwin.h>
  8. #include "Resource.h"
  9. #include "DlgDemo5.h"
  10.  
  11. CMyApp myApp;
  12.  
  13. /////////////////////////////////////////////////////////////////////////
  14. // CMyApp member functions
  15.  
  16. BOOL CMyApp::InitInstance ()
  17. {
  18.     m_pMainWnd = new CMainWindow;
  19.     m_pMainWnd->ShowWindow (m_nCmdShow);
  20.     m_pMainWnd->UpdateWindow ();
  21.     return TRUE;
  22. }
  23.  
  24. /////////////////////////////////////////////////////////////////////////
  25. // CMainWindow message map and member functions
  26.  
  27. BEGIN_MESSAGE_MAP (CMainWindow, CFrameWnd)
  28.     ON_WM_ERASEBKGND ()
  29.     ON_WM_PAINT ()
  30.     ON_COMMAND (IDM_OPTIONS_SETTINGS, OnOptionsSettings)
  31.     ON_COMMAND (IDM_OPTIONS_EXIT, OnOptionsExit)
  32.     ON_COMMAND (IDM_OPTIONS_ABOUT, OnOptionsAbout)
  33.     ON_MESSAGE (WM_USER, OnApplySettings)
  34. END_MESSAGE_MAP ()
  35.  
  36. CMainWindow::CMainWindow ()
  37. {
  38.     m_pDlg = NULL;
  39.  
  40.     m_nFillType = 1;
  41.     m_nFillColor = 2;
  42.     m_text = "Hello, MFC";
  43.     m_nHeight = 72;
  44.     m_bBold = TRUE;
  45.     m_bItalic = TRUE;
  46.  
  47.     Create (NULL, "DlgDemo5", WS_OVERLAPPEDWINDOW, rectDefault, NULL,
  48.         MAKEINTRESOURCE (IDR_MAINFRAME));
  49. }
  50.  
  51. BOOL CMainWindow::OnEraseBkgnd (CDC* pDC)
  52. {
  53.     CRect rect;
  54.     GetClientRect (&rect);
  55.  
  56.     m_nFillType == 1 ? DoGradientFill (pDC, &rect) :
  57.         DoSolidFill (pDC, &rect);
  58.     return TRUE;
  59. }
  60.  
  61. void CMainWindow::OnPaint ()
  62. {
  63.     CRect rect;
  64.     GetClientRect (&rect);
  65.  
  66.     CPaintDC dc (this);
  67.     DoDrawText (&dc, &rect);
  68. }
  69.  
  70. void CMainWindow::OnOptionsSettings ()
  71. {
  72.     if (m_pDlg != NULL) {
  73.         m_pDlg->SetFocus ();
  74.         return;
  75.     }
  76.  
  77.     m_pDlg = new CSettingsDialog;
  78.  
  79.     m_pDlg->m_nFillType = m_nFillType;
  80.     m_pDlg->m_nFillColor = m_nFillColor;
  81.     m_pDlg->m_text = m_text;
  82.     m_pDlg->m_nHeight = m_nHeight;
  83.     m_pDlg->m_bBold = m_bBold;
  84.     m_pDlg->m_bItalic = m_bItalic;
  85.  
  86.     m_pDlg->Create (IDD_SETTINGSDLG, this);
  87. }
  88.  
  89. void CMainWindow::OnOptionsExit ()
  90. {
  91.     SendMessage (WM_CLOSE, 0, 0);
  92. }
  93.  
  94. void CMainWindow::OnOptionsAbout ()
  95. {
  96.     CAboutDialog dlg (this);
  97.     dlg.DoModal ();
  98. }
  99.  
  100. LONG CMainWindow::OnApplySettings (UINT wParam, LONG lParam)
  101. {
  102.     PPROPERTIES pProp = (PPROPERTIES) lParam;
  103.  
  104.     m_nFillType = pProp->nFillType;
  105.     m_nFillColor = pProp->nFillColor;
  106.     m_text = pProp->text;
  107.     m_nHeight = pProp->nHeight;
  108.     m_bBold = pProp->bBold;
  109.     m_bItalic = pProp->bItalic;
  110.  
  111.     Invalidate ();
  112.     return 0;
  113. }
  114.  
  115. void CMainWindow::DoSolidFill (CDC* pDC, CRect* pRect)
  116. {
  117.     static COLORREF crColor[5] = {
  118.         RGB (255,   0,   0),
  119.         RGB (  0, 255,   0),
  120.         RGB (  0,   0, 255),
  121.         RGB (255,   0, 255),
  122.         RGB (  0, 255, 255),
  123.     };
  124.  
  125.     CBrush brush (crColor[m_nFillColor]);        
  126.     pDC->FillRect (pRect, &brush);
  127. }
  128.  
  129. void CMainWindow::DoGradientFill (CDC* pDC, CRect* pRect)
  130. {
  131.     CBrush* pBrush[64];
  132.     for (int i=0; i<64; i++) {
  133.         switch (m_nFillColor) {
  134.  
  135.         case 0:
  136.             pBrush[i] = new CBrush (RGB (255 - (i * 4), 0, 0));
  137.             break;
  138.  
  139.         case 1:
  140.             pBrush[i] = new CBrush (RGB (0, 255 - (i * 4), 0));
  141.             break;
  142.  
  143.         case 2:
  144.             pBrush[i] = new CBrush (RGB (0, 0, 255 - (i * 4)));
  145.             break;
  146.  
  147.         case 3:
  148.             pBrush[i] = new CBrush (RGB (255 - (i * 4), 0,
  149.                 255 - (i * 4)));
  150.             break;
  151.  
  152.         case 4:
  153.             pBrush[i] = new CBrush (RGB (0, 255 - (i * 4),
  154.                 255 - (i * 4)));
  155.             break;
  156.         }
  157.     }
  158.  
  159.     int nWidth = pRect->Width ();
  160.     int nHeight = pRect->Height ();
  161.     CRect rect;
  162.  
  163.     for (i=0; i<nHeight; i++) {
  164.         rect.SetRect (0, i, nWidth, i + 1);
  165.         pDC->FillRect (&rect, pBrush[(i * 63) / nHeight]);
  166.     }
  167.  
  168.     for (i=0; i<64; i++)
  169.         delete pBrush[i];
  170. }
  171.  
  172. void CMainWindow::DoDrawText (CDC* pDC, CRect* pRect)
  173. {
  174.     CFont font;
  175.     int nHeight = -((pDC->GetDeviceCaps (LOGPIXELSY) * m_nHeight) / 72);
  176.  
  177.     font.CreateFont (nHeight, 0, 0, 0, m_bBold ? FW_BOLD : FW_NORMAL,
  178.         m_bItalic, 0, 0, DEFAULT_CHARSET, OUT_CHARACTER_PRECIS,
  179.         CLIP_CHARACTER_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH |
  180.         FF_DONTCARE, "Times New Roman");
  181.  
  182.     pDC->SetBkMode (TRANSPARENT);
  183.     pDC->SetTextColor (RGB (255, 255, 255));
  184.  
  185.     CFont* pOldFont = pDC->SelectObject (&font);
  186.     pDC->DrawText (m_text, -1, pRect, DT_SINGLELINE | DT_CENTER |
  187.         DT_VCENTER);
  188.  
  189.     pDC->SelectObject (pOldFont);
  190. }
  191.  
  192. /////////////////////////////////////////////////////////////////////////
  193. // CSettingsDialog message map and member functions
  194.  
  195. BEGIN_MESSAGE_MAP (CSettingsDialog, CDialog)
  196.     ON_BN_CLICKED (IDC_DEFAULTS, OnDefaults)
  197. END_MESSAGE_MAP ()
  198.  
  199. void CSettingsDialog::OnOK ()
  200. {
  201.     UpdateData (TRUE);
  202.  
  203.     PROPERTIES prop;
  204.     prop.nFillType = m_nFillType;
  205.     prop.nFillColor = m_nFillColor;
  206.     prop.text = m_text;
  207.     prop.nHeight = m_nHeight;
  208.     prop.bBold = m_bBold;
  209.     prop.bItalic = m_bItalic;
  210.  
  211.     AfxGetMainWnd ()->SendMessage (WM_USER, 0, (LPARAM) &prop);
  212. }
  213.  
  214. void CSettingsDialog::OnCancel ()
  215. {
  216.     DestroyWindow ();
  217. }
  218.  
  219. void CSettingsDialog::PostNcDestroy ()
  220. {
  221.     ((CMainWindow*) AfxGetMainWnd ())->m_pDlg = NULL;
  222.     delete this;
  223. }
  224.  
  225. void CSettingsDialog::OnDefaults ()
  226. {
  227.     m_nFillType = 1;
  228.     m_nFillColor = 2;
  229.     m_text = "Hello, MFC";
  230.     m_nHeight = 72;
  231.     m_bBold = TRUE;
  232.     m_bItalic = TRUE;
  233.  
  234.     UpdateData (FALSE);
  235. }
  236.  
  237. void CSettingsDialog::DoDataExchange (CDataExchange* pDX)
  238. {
  239.     CDialog::DoDataExchange (pDX);
  240.  
  241.     DDX_Radio (pDX, IDC_SOLID, m_nFillType);
  242.     DDX_Radio (pDX, IDC_RED, m_nFillColor);
  243.     DDX_Text (pDX, IDC_TEXT, m_text);
  244.     DDX_Text (pDX, IDC_HEIGHT, m_nHeight);
  245.     DDV_MinMaxInt (pDX, m_nHeight, 8, 144);
  246.     DDX_Check (pDX, IDC_BOLD, m_bBold);
  247.     DDX_Check (pDX, IDC_ITALIC, m_bItalic);
  248. }
  249.  
  250. /////////////////////////////////////////////////////////////////////////
  251. // CAboutDialog message map and member functions
  252.  
  253. BEGIN_MESSAGE_MAP (CAboutDialog, CDialog)
  254.     ON_WM_PAINT ()
  255. END_MESSAGE_MAP ()
  256.  
  257. BOOL CAboutDialog::OnInitDialog ()
  258. {
  259.     CDialog::OnInitDialog ();
  260.  
  261.     CStatic* pStatic = (CStatic*) GetDlgItem (IDC_ICONRECT);
  262.     pStatic->GetWindowRect (&m_rect);
  263.     pStatic->DestroyWindow ();
  264.     ScreenToClient (&m_rect);
  265.  
  266.     return TRUE;
  267. }
  268.  
  269. void CAboutDialog::OnPaint ()
  270. {
  271.     CPaintDC dc (this);
  272.     HICON hIcon = (HICON) ::GetClassLong (AfxGetMainWnd ()->m_hWnd,
  273.         GCL_HICON);
  274.  
  275.     if (hIcon != NULL) {
  276.         CDC dcMem;
  277.         dcMem.CreateCompatibleDC (&dc);
  278.  
  279.         CBitmap bitmap;
  280.         bitmap.CreateCompatibleBitmap (&dc, 32, 32);
  281.         CBitmap* pOldBitmap = dcMem.SelectObject (&bitmap);
  282.  
  283.         CBrush brush (::GetSysColor (COLOR_3DFACE));
  284.         dcMem.FillRect (CRect (0, 0, 32, 32), &brush);
  285.         dcMem.DrawIcon (0, 0, hIcon);
  286.  
  287.         dc.StretchBlt (m_rect.left, m_rect.top, m_rect.Width(),
  288.             m_rect.Height (), &dcMem, 0, 0, 32, 32, SRCCOPY);
  289.  
  290.         dcMem.SelectObject (pOldBitmap);
  291.     }
  292. }
  293.